package com.controller.admin;

import com.entity.Blog;
import com.entity.BlogType;
import com.entity.Blogger;
import com.service.BlogService;
import com.service.BlogTypeService;
import com.service.BloggerService;
import com.util.ResponseUtil;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 系统Controller层
 * @author Administrator
 *
 */

@Controller
@RequestMapping("/admin/system")
public class SystemController {

    @Resource
    private BloggerService bloggerService;

    @Resource
    private BlogTypeService blogTypeService;

    @Resource
    private BlogService blogService;
    /**
     * 刷新缓存
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/flushSystem")
    public String refreshSystem(HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletContext application= RequestContextUtils.getWebApplicationContext(request).getServletContext();

//        Blogger blogger=bloggerService.find();//获取博主信息
//        blogger.setPassword("");
//        application.setAttribute("blogger", blogger);//博主信息放入application

        List<BlogType> blogTypeCountList=blogTypeService.countList(); // 查询博客类别以及博客的数量
        application.setAttribute("blogTypeCountList", blogTypeCountList);

        List<Blog> blogCountList=blogService.countList(); // 根据日期分组查询博客
        application.setAttribute("blogCountList", blogCountList);

        JSONObject result=new JSONObject();
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }
}
